<?php

class Core_Component_System_Business_Dms_Form {

    private $_model = NULL;
    private $_form = NULL;
    private $_dataSources = array();
    
    public function __construct(){
        $this->_form = new Soul_Template_Html_Form();
        $this->_form->setAction($GLOBALS[RC]->getRouter()->buildUrl('*/*/*/save'))->setId('form');
    }
    
    public function getForm(){
        return $this->_form;
    }   
    
    public function getModel(){
        return $this->_model;
    }
    
    public function setModel(Soul_Model_Entity_Base $value){
        $this->_model = $value;
        $this->getForm()->setData($this->_model->getAllData());
        return $this;
    }
    
    public function setDataSources(array $value){
        $this->_dataSources = $value;
        return $this;
    }
    
    public function getDataSources(){
        return $this->_dataSources;
    }
    
    public function buildField($property){
        $lang = $GLOBALS[RC]->getLanguage();
        $property = is_string($property)? $this->getModel()->getProperty($propertyName):$property;
        switch ($property->getInputType()) {
            case 'textbox':
                $input = new Soul_Template_Html_Form_Textbox();
                switch ($property->getPropertyType()) {
                    case 'integer':
                        $input->addDigitsRule();
                        break;
                    case 'float':
                        $input->addNumberRule();
                        break;
                }
                break;
            case 'textarea':
                $input = new Soul_Template_Html_Form_Textarea();
                break;                
            case 'boolean':
                $input = new Core_Template_Html_Form_Boolean();
                break;
            
                $input = new Soul_Template_Html_Form_Select();
                
                if (isset($this->_dataSources[$property->getName()])) {
                    $ds = $this->_dataSources[$property->getName()];
                    $input->bindData($ds['data'], $ds['labelKey'], $ds['valueKey']);
                }
                break;
            case 'email':
                $input = new Soul_Template_Html_Form_Email();
                break;
            case 'datetime':
                $input = new Soul_Template_Html_Form_Datetime();
                break;            
            case 'select':
                $input = new Soul_Template_Html_Form_Select();
                if($property->getPropertyType() == 'array'){
                    $input->setMultiple(TRUE);
                }else{
                    $input->setOption($lang->__('system.label.default_option'), '');
                }
                if (isset($this->_dataSources[$property->getName()])) {                    
                    $ds = $this->_dataSources[$property->getName()];
                    if(isset($ds['labelFormat'])){
                        $input->setLabelFormat($ds['labelFormat']);
                    }
                    if(isset($ds['valueFormat'])){
                        $input->setValueFormat($ds['valueFormat']);
                    }                     
                    $input->bindData($ds['data'], $ds['labelKey'], $ds['valueKey']);                   
                }
                break;
            case 'hidden':
                $input = new Soul_Template_Html_Form_Hidden();
                break;                
            case 'treeselect':
                $input = new Soul_Template_Html_Form_Select_Tree();
                if($property->getPropertyType() == 'array'){
                    $input->setMultiple(TRUE);
                }else{
                    $input->setOption($lang->__('system.label.default_option'), '');
                }
                if (isset($this->_dataSources[$property->getName()])) {                    
                    $ds = $this->_dataSources[$property->getName()];
                    if(isset($ds['labelFormat'])){
                        $input->setLabelFormat($ds['labelFormat']);
                    }
                    if(isset($ds['valueFormat'])){
                        $input->setValueFormat($ds['valueFormat']);
                    }                    
                    $startIndex = isset($ds['startIndex'])? $ds['startIndex']: 0;
                    $setting = isset($ds['setting'])?$ds['setting']:array('childKey'=>'id', 'parentKey'=>'parent_id', 'labelKey'=>'name', 'valueKey'=>'id');
                    $input->bindData($ds['data'], $startIndex, $setting);
                }
                break;                
            case 'editor':
                $input = new Core_Template_Html_Form_Editor();
                break;  
            case 'imageupload':
                $this->getForm()->setEnctype('multipart/form-data');
                $input = new Core_Template_Html_Form_Upload_Image();
                if($property->getPropertyType() == 'array'){
                    if($property->getItemCount() > 0){
                        $input->setCount($property->getItemCount());
                    }
                }
                break;            
        }
        
        $field = $this->getForm()->createField();
        $input->setId($property->getName())
              ->setName('model['.$property->getName().']');
        if($input instanceof Soul_Template_Html_Form_Hidden){
            $field->setStyle('display', 'none');
        }else{
            if($property->getLength()){
                $input->addMaxLengthRule($property->getLength());
            }

            $label = $property->getLabel();
            $desc = $property->getDescription();
            if(!empty($label)){
                $label = $lang->__($label);
            }
            if(!empty($desc)){
                $desc = $lang->__($desc);
            }
            $field->setLabel($label)
                  ->setDescription($desc)
                  ->setRequired($property->getRequired())
                  ->setId('field_'.$property->getName());
        }
        $field->setInput($input);
        return $field;
    }
    
    public function buildInsertableFields(){
        foreach($this->getModel()->getProperties() as $property){
            if($property->getInsertable() && $property->getInputType()){
                $field = $this->buildField($property);
                if(!is_null($field)){
                    $field->getInput()->setValue($property->getDefaultValue());
                }
            }
        }
        return $this;
    }    
    
    public function buildUpdateableFields(){
        foreach($this->getModel()->getProperties() as $property){
            if($property->getUpdateable() && $property->getInputType()){
                $this->buildField($property);
            }
        }
        $hidden = new Soul_Template_Html_Form_Hidden();
        $idProperty = $this->getModel() instanceof Soul_Model_Entity_Version?
                        $this->getModel()->getVersionIdProperty():
                        $this->getModel()->getIdProperty();
        $hidden->setId($idProperty->getName())->setName('model['.$idProperty->getName().']');
        $this->getForm()
                ->createField('', FALSE)
                ->setInput($hidden)
                ->setStyle('display', 'none');
        return $this;
    }
    
    public function buildForm(){
        $this->getModel()->isNew()? $this->buildInsertableFields(): $this->buildUpdateableFields();
        return $this->getForm();
    }
}